
#include <linux/fs.h>
#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/delay.h>
#include <linux/ide.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/errno.h>
#include <linux/gpio.h>
#include <linux/cdev.h>
#include <linux/device.h>
#include <linux/of.h>
#include <linux/of_address.h>
#include <linux/of_gpio.h>
#include <linux/timer.h>
#include <linux/platform_device.h>
#include <asm/mach/map.h>
#include <asm/uaccess.h>
#include <asm/io.h>

#define LEDDEV_CNT            1
#define CHRDEV_NAME             "led"
#define LED_OFF                 0
#define LED_ON                  1

#define OPEN_TIMER              2
#define CLOSE_TIMER             3

struct led_dev{
    dev_t devid; /* 设备号 */
    struct cdev cdev; /* cdev */
    struct class *class; /* 类 */
    struct device *device; /* 设备 */
    int major; /* 主设备号 */
    int minor; /* 次设备号 */
    struct device_node *node;     /*设备节点*/
    int led_gpio;       /* led所使用的gpio编号*/
    unsigned int state; /*led 开关状态*/
#ifdef TIMER
    struct timer_list timer;     /* 定时器*/
    int period;
#endif
    atomic_t lock;     /* 原子变量*/
};

struct led_dev leddev;


static int led_open(struct inode *inode, struct file *filp)
{
    if (!atomic_dec_and_test(&leddev.lock))
    {
        atomic_inc(&leddev.lock);
        return -EBUSY;
    }


    filp->private_data = &leddev; /* 设置私有数据 */
#ifdef TIMER
    leddev.period = 1000;
#endif
    return 0;
}

static ssize_t led_read(struct file *filp, 
                        char __user *buf, 
                        size_t cnt, 
                        loff_t *offt)
{
    return 0;
}

static ssize_t led_write(struct file *filp, 
                         const int __user *buf,
                         size_t cnt, 
                         loff_t *offt)
{
    int retvalue;
    unsigned int databuf;
    struct led_dev *dev = filp->private_data;

    retvalue = copy_from_user(&databuf, buf, cnt);
    if(retvalue < 0)
    {
        printk("kernel write failed!\r\n");
        return -EFAULT;
    }

    dev->state = databuf; /* 获取状态值 */

#ifdef TIMER
    if (dev->state >= 1000)
    {
        /* 设置定时器周期*/
        dev->period = dev->state;
        mod_timer(&dev->timer, jiffies + msecs_to_jiffies(dev->period));
        return 0;
    }
#endif

    switch(dev->state) 
    {
        case LED_ON:
            gpio_set_value(dev->led_gpio, 0);
            break;
        case LED_OFF:
            gpio_set_value(dev->led_gpio, 1);
#ifdef TIMER
            del_timer_sync(&dev->timer);
            break;
        case OPEN_TIMER:
            mod_timer(&dev->timer, jiffies + msecs_to_jiffies(dev->period));
            break;
        case CLOSE_TIMER:
            del_timer_sync(&dev->timer);
#endif
            break;
        default:
            break;
    }

    return 0;
}

static int led_release(struct inode *inode, struct file *filp)
{
    struct led_dev *dev = filp->private_data;
    atomic_inc(&dev->lock);

    return 0;
}

static struct file_operations leddev_fops = {
    .owner = THIS_MODULE,
    .open = led_open,
    .read = led_read,
    .write = led_write,
    .release = led_release,
};

#ifdef TIMER
void timer_handle(void)
{
    /*int value = 0;*/
    /*value = __gpio_get_value(leddev.led_gpio);*/
    leddev.state = !leddev.state;
    gpio_set_value(leddev.led_gpio, leddev.state);

    mod_timer(&leddev.timer, jiffies + msecs_to_jiffies(leddev.period));
}
#endif

static int leds_probe(struct platform_device *dev)
{
    int ret;

    atomic_set(&leddev.lock, 1);   /* 原子变量初始值为1*/

    leddev.node = of_find_node_by_path("/gpioled4");
    if (leddev.node)
    {
        printk("led node found success!");
    }
    else
    {
        printk("led found failed!!!");
        return -EINVAL;
    }

    leddev.led_gpio = of_get_named_gpio(leddev.node, "led-gpio", 0);
    if (leddev.led_gpio < 0)
    {
        printk("compatible found failed!!!");
        return -EINVAL;
    }
    printk("led4-gpio num = %d\r\n", leddev.led_gpio);

    gpio_request(leddev.led_gpio, "ledgpio");
    ret = gpio_direction_output(leddev.led_gpio, 1);
    if (ret < 0)
    {
        printk("cannot set gpio!\r\n");
    }

    /* 注册字符设备驱动 */
    /* 1、创建设备号 */
    if (leddev.major)
    {
        leddev.devid = MKDEV(leddev.major, 0);
        register_chrdev_region(leddev.devid, LEDDEV_CNT, CHRDEV_NAME);
    }
    else
    {
        alloc_chrdev_region(&leddev.devid, 0, LEDDEV_CNT, CHRDEV_NAME); /* 申请设备号 */
        leddev.major = MAJOR(leddev.devid); /* 获取主设备号 */
        leddev.minor = MINOR(leddev.devid); /* 获取次设备号 */
    }

    printk("leddev major=%d,minor=%d\r\n",leddev.major, leddev.minor);

    /* 2、初始化 cdev */
    leddev.cdev.owner = THIS_MODULE;
    cdev_init(&leddev.cdev, &leddev_fops);

    /* 3、添加一个 cdev */
    cdev_add(&leddev.cdev, leddev.devid, LEDDEV_CNT);

    /* 4、创建类 */
    leddev.class = class_create(THIS_MODULE, CHRDEV_NAME);
    if (IS_ERR(leddev.class))
    {
        return PTR_ERR(leddev.class);
    }

    /* 5、创建设备 */
    leddev.device = device_create(leddev.class, NULL, leddev.devid, NULL, CHRDEV_NAME);
    if (IS_ERR(leddev.device))
    {
        return PTR_ERR(leddev.device);
    }

#ifdef TIMER
    /* 初始化定时器 */
    init_timer(&leddev.timer);
    leddev.timer.function = timer_handle;
#endif

    return 0;
}

static int leds_remove(struct platform_device *dev)
{
#ifdef TIMER
    /* 删除定时器 */
    del_timer_sync(&leddev.timer);
#endif

    /*释放gpio*/
    gpio_free(leddev.led_gpio);

    /* 注销字符设备 */
    cdev_del(&leddev.cdev);/* 删除 cdev */
    unregister_chrdev_region(leddev.devid, LEDDEV_CNT);

    device_destroy(leddev.class, leddev.devid);
    class_destroy(leddev.class);

    return 0;
}

static const struct of_device_id leds_of_match[] = {
    { .compatible = "gpio-led4" },
    { /* Sentinel */ }
};

static struct platform_driver leds_platform_driver = {
    .driver = {
        .name = "imx6ul-led",
        .of_match_table = leds_of_match,
    },
    .probe = leds_probe,
    .remove = leds_remove,
};

static int __init led_init(void)
{
    return platform_driver_register(&leds_platform_driver);
}

static void __exit led_exit(void)
{
    platform_driver_unregister(&leds_platform_driver);
}


module_init(led_init);
module_exit(led_exit);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("xue");


